const express = require('express'),
      settingsService = require('../services/settingsService'),
      envService = require('../services/envService');

const settingsRouter = express.Router();

settingsRouter
    .route('/')

    .get((req, res, next) => {
        try {
            const settings = settingsService.load();
            res.json(settings);
        } catch(err) {
            next(err);
        }
    })
    
    .post((req, res, next) => {
        try {
            const data = req.body;

            settingsService.save(data);
            envService.update();

            res.sendStatus(200);
        } catch(err) {
            next(err);
        }
    });

module.exports = settingsRouter;
